package com.hxd.otc.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.dialog.widget.base.BaseDialog;
import com.hxd.otc.R;
import com.hxd.otc.common.SpConfig;
import com.hxd.otc.module.my.bean.AppInfoBean;
import com.hxd.otc.utils.SpUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lzy on 2018/7/25 0025.
 * description ${版本升级对话框}
 */
public class UpdateVersionDialog extends BaseDialog<UpdateVersionDialog> implements DialogInterface.OnDismissListener {
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_content)
    TextView mTvContent;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    @BindView(R.id.iv_logo)
    ImageView mIvLogo;
    @BindView(R.id.iv_close)
    ImageView mIvClose;

    private AppInfoBean mAppInfoBean;

    public UpdateVersionDialog(Context context) {
        super(context);
    }

    @Override
    public View onCreateView() {
        widthScale(0.797f);
        return View.inflate(getContext(), R.layout.dialog_update_version, null);
    }

    @Override
    public void onViewCreated(View view) {
        ButterKnife.bind(this, view);
    }

    @Override
    public void setUiBeforShow() {
        String comment = mAppInfoBean.getComment();
        if (!TextUtils.isEmpty(comment)) {
            comment = comment.replace("\\n", "\n");
        }
        mTvContent.setText(comment);
        boolean forceUpdate = mAppInfoBean.isForceUpdate();
        mIvClose.setVisibility(forceUpdate ? View.GONE : View.VISIBLE);
        setOnDismissListener(forceUpdate ? null : this);
        setCanceledOnTouchOutside(!forceUpdate);
    }

    @Override
    public void onBackPressed() {
        if (!mAppInfoBean.isForceUpdate()) {
            super.onBackPressed();
        }
    }

    public UpdateVersionDialog setAppInfo(AppInfoBean appInfoBean) {
        mAppInfoBean = appInfoBean;
        return this;
    }

    @OnClick({R.id.tv_confirm, R.id.iv_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.tv_confirm:
                onDownloadApk();
                break;
        }
    }

    private void onDownloadApk() {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri content_url = Uri.parse(mAppInfoBean.getAddress());
        intent.setData(content_url);
        getContext().startActivity(intent);
        if (!mAppInfoBean.isForceUpdate()) {
            dismiss();
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if (!mAppInfoBean.isForceUpdate()) {
            SpUtil.PERMANENT.putInt(SpConfig.Permanent.IGNORE_VERSION_CODE, mAppInfoBean.getVersionValue());
        }
    }
}
